//
//  ZDBaseTableViewController.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/10/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDBaseTableViewController.h"
#import "ZDVisitorView.h"
#import "ZDHomeViewController.h"
#import "ZDMessageViewController.h"
#import "ZDDiscoveryViewController.h"
#import "ZDProfileViewController.h"
#import "ZDAuthViewController.h"
#import "ZDUserAccount.h"

@interface ZDBaseTableViewController () <ZDVisitorViewDelegate>

////记录用户的登录状态
//@property (nonatomic, assign, getter=isLogin) BOOL login;

@end

@implementation ZDBaseTableViewController

+ (void)initialize
{
    if ([self isSubclassOfClass:[ZDBaseTableViewController class]])
    {
        UINavigationBar *bar = [UINavigationBar appearance];
        
        bar.tintColor = [UIColor orangeColor];
    }
}

//重写loadView 根据用户登录状态 决定显示tableView还是登录提示界面
- (void)loadView
{
//    if (self.isLogin)   //如果是登录状态，就显示tableView
//    {
//        [super loadView];
//    }
//    else
//    {
//        self.view = [self setUpVisitorView];
//    }
    
    [ZDUserAccount isLogin] ? [super loadView] : [self setUpVisitorView];
}


#pragma mark - 设置访客视图
- (void)setUpVisitorView
{
    ZDVisitorView *visitorView = [[ZDVisitorView alloc] init];
    
    visitorView.delegate = self;
    
    self.view = visitorView;
    
    if ([self isKindOfClass:[ZDHomeViewController class]])
    {
        //开始旋转
        [visitorView startRotation];
        
        //注册通知 监听应用进入后台
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
        //注册通知 监听应用进入前台
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    }
    else if ([self isKindOfClass:[ZDMessageViewController class]])
    {
        [visitorView customVisitorView:@"visitordiscover_image_message" andTitle:@"登录后，别人评论你的微博，发给你的消息，都会在这里收到通知"];
    }
    else if ([self isKindOfClass:[ZDDiscoveryViewController class]])
    {
        [visitorView customVisitorView:@"visitordiscover_image_message" andTitle:@"登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"];
    }
    else if ([self isKindOfClass:[ZDProfileViewController class]])
    {
        [visitorView customVisitorView:@"visitordiscover_image_profile" andTitle:@"登录后，你的微博、相册、个人资料会显示在这里，展示给别人"];
    }
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStyleDone target:self action:@selector(registerItemClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStyleDone target:self action:@selector(loginItemClick)];
}


#pragma mark - 通知回调方法
- (void)applicationDidEnterBackground
{
    ZDVisitorView *visitorView = (ZDVisitorView *)self.view;
    
    //暂停旋转
    [visitorView pauseAnimation];
}

- (void)applicationDidBecomeActive
{
    ZDVisitorView *visitorView = (ZDVisitorView *)self.view;
    
    //暂停旋转
    [visitorView resumeAnimation];
}

#pragma mark - 导航栏按钮点击事件
- (void)registerItemClick
{
    
}

- (void)loginItemClick
{
    ZDAuthViewController *vc = [[ZDAuthViewController alloc] init];
    
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}


#pragma mark - 实现代理方法
- (void)visitorView:(ZDVisitorView *)visitorView didClickButton:(ZDButtonType)type
{
    if (type == ZDButtonTypeRegister)
    {
        
    }
    else if (type == ZDButtonTypeLogin)
    {
        ZDLog(@"%s", __func__);
        
        [self loginItemClick];
    }
}


@end
